package com.example.mideaoem.data;

import android.util.Log;

import com.example.mideaoem.data.status.IDataBodyDevStauts;
import com.example.mideaoem.model.ApplianceInfo;
import com.example.mideaoem.utils.TemperatureUtil;

public class DataBodyDevOld extends FactoryDataBody {

	private byte commandType;

	public DataBodyDevOld(int type) {
		this.commandType = (byte) type;
	}

	public DataBodyDevOld() {
		this.commandType = (byte) 0x40;
	}

	/**
	 * 第零个byte
	 */
	byte Zerobyte;

	/**
	 * 第一个byte
	 */
	byte FirstByte;

	byte powerStatus; // 开机状态
	byte controlSrc = 0x01; // 控制来源
	byte imodeResume; // i模式恢复
	byte childSleepMode; // 儿童睡眠模式
	byte timerMode; // 定时模式
	byte test2; // 快检
	byte keyStatus = 0x01; // 按键状态

	/**
	 * 第二个byte
	 */
	byte Secondbyte;
	int setTemperature; // 设定温度
	byte setTemperature_dot;// 设定温度-小数 0.5
	byte mode; // 模式 自动-0x20 制冷-0x40 抽湿-0x60 制热-0x80 送风-0xa0

	/**
	 * 第三个byte
	 */
	byte Thirdbyte;
	byte fanSpeed; // 风速 固定风-0x65
	byte timerEffe; // 移动终端定时有效

	/**
	 * 第四个byte
	 */
	byte Fourthbyte;
	byte timer_on_minL; // 定时开-分 低二位
	byte timer_on_hour; // 定时开-时
	byte timer_on; // 定时开功能

	/**
	 * 第五个byte
	 */
	byte Fifthbyte;
	byte timer_off_minL; // 定时关-分 低二位
	byte timer_off_hour; // 定时关-时
	byte timer_off; // 定时关功能

	/**
	 * 第六个byte
	 */
	byte Sixthbyte;
	byte timer_off_minH; // 定时关-分 高四位
	byte timer_on_minH; // 定时开-分 高四位

	/**
	 * 第七个byte ？
	 */
	byte Seventhbyte;
	byte keyBtn; // 实体键控制
	byte updownFan; // 上下风
	byte leftRightFan; // 左右风

	/**
	 * 第八个byte 舒睡 强劲 睿风
	 */
	byte Eighthbyte;
	byte cosySleep; // 舒睡 00-无舒睡 01-舒睡1 02-舒睡2 03-舒睡3
	byte alarmSleep; // 闹铃睡眠
	byte save; // 省电
	byte lowFerqFan; // 睿风
	byte superFan; // 强劲
	byte powerSaver; // 节能
	byte feelOwn; // 随身感

	/**
	 * 第九个byte 自然风 干燥 电辅热 ECO 随身感 本机熟睡
	 */
	byte Ninethbyte;
	byte wiseEye; // 智慧眼
	byte exchangeAir; // 换气
	byte dryClean; // 干燥
	byte ptcAssis; // 电辅热
	byte ptcButton; // 电辅热按钮
	byte cleanUp; // 净化
	byte changeCosySleep; // 切换舒睡曲线
	byte eco; // ECO

	/**
	 * 第十个byte 换气 小夜灯 防着凉 峰谷省电
	 */
	byte Tenthbyte;
	byte sleepFunc; // Sleep功能状态
	byte tubro; // TUBRO涡轮
	byte tempUnit; // 温度单位
	byte catchCold; // 防着凉
	byte nightLight; // 小夜灯
	byte peakElec; // 峰谷节电
	byte dusFull; // 满尘
	byte cleanFanTime; // 清除风机运行时间

	/**
	 * 第十一个byte
	 */
	byte Eleventhbyte;

	/**
	 * 第十二个byte
	 */
	byte Twelfthbyte;

	/**
	 * 第十三个byte
	 */
	byte Thirteenthbyte;

	/**
	 * 第十四个byte
	 */
	byte Fourteenthbyte;

	/**
	 * 第十五个byte
	 */
	byte Fifteenthbyte;

	/**
	 * 第十六个byte
	 */
	byte Sixteenthbyte;

	/**
	 * 第十七个byte
	 */
	byte Seventeenthbyte;
	byte naturalFan; // 自然风
	byte pmvFunc_H; // PMV功能

	/**
	 * 第十八个byte
	 */
	byte Eighteenthbyte;
	byte pmvFunc_L; // PMV功能

	/**
	 * 第19byte
	 */
	byte Nineteenthbyte;

	/**
	 * 第20byte
	 */
	byte Twentieth;

	/**
	 * 第21byte
	 */
	byte Twenty_first;
	byte setExpand_dot; // 扩展温度-小数
	byte setExpand; // 扩展温度
	byte double_temp; // 双温控制
	byte Eight_Hot; // 8度制热

	private static byte number = 4; // 累加发送帧

	/********* 0x41命令 **********/
	// byte 3
	private byte powerUsage = 0x04;
	private byte readCmd = 0x40;
	private byte writeCmd = (byte) 0x80;

	private byte setCmd;// 电量设置命令
	private byte clearCmd;// 清零命令
	private byte resetCmd;// 矫正命令
	private byte addupCmd;// 补偿命令
	private byte runmodeCmd;// 电量运行模式命令
	private byte setallusageCmd;// 设置电量总量命令

	private byte powerValue1;//
	private byte powerValue2;//
	private byte powerValue3;//

	private byte time1;
	private byte timeLimit;// 时间限制
	private byte time3;

	public byte[] toBytes(boolean isMode) {
		return toBytes(isMode, false);
	}

	public byte[] toBytes(boolean isMode, boolean meco) {
		Log.d("rawData", "217  eco = " + eco);
		if (isMode) {
			if (meco) {
				eco = IDataBodyDevStauts.OFF;
			}
			tubro = IDataBodyDevStauts.OFF;
			leftRightFan = IDataBodyDevStauts.OFF;
			updownFan = IDataBodyDevStauts.OFF;
			Eight_Hot = IDataBodyDevStauts.OFF;
			feelOwn = IDataBodyDevStauts.OFF;
			naturalFan = IDataBodyDevStauts.OFF;
			catchCold = IDataBodyDevStauts.OFF;
			cosySleep = IDataBodyDevStauts.OFF;
			sleepFunc = IDataBodyDevStauts.OFF;
		}

		if (commandType == 0x40) {

			if (powerStatus == 0) {
				eco = IDataBodyDevStauts.OFF;
				tubro = IDataBodyDevStauts.OFF;
			}

			Log.d("rawData", "245  eco = " + eco);
			// if(mode == IDataBodyDevStauts.MODE_HEAT){
			// tubro = IDataBodyDevStauts.OFF;
			// }
			if (mode == IDataBodyDevStauts.MODE_WIND) {
				eco = IDataBodyDevStauts.OFF;
				tubro = IDataBodyDevStauts.OFF;
			}
			if (mode == IDataBodyDevStauts.MODE_DRY && fanSpeed == 102) {
				fanSpeed = 101;
			}
			if (mode != IDataBodyDevStauts.MODE_DRY && fanSpeed == 101) {
				fanSpeed = 102;
			}

			if (powerStatus == IDataBodyDevStauts.OFF) {
				eco = IDataBodyDevStauts.OFF;
				tubro = IDataBodyDevStauts.OFF;
				leftRightFan = IDataBodyDevStauts.OFF;
				updownFan = IDataBodyDevStauts.OFF;
			}

			byte[] bytes;

			bytes = new byte[25];

			Zerobyte = (byte) 0x40;
			FirstByte = (byte) (powerStatus | (0x02) | (imodeResume & 0x04) | (childSleepMode & 0x08)
					| (timerMode & 0x10) | (test2 & 0x20) | (0x40));
			Log.d("rawData", "275  eco = " + eco);

			/** 判断 设置温度 */
			if (setTemperature >= 50) {
				String CTemp = TemperatureUtil.Fahrenheit2centigrade(setTemperature + ".0");
				if (CTemp.contains(".5")) {
					setTemperature_dot = 1;
				}
				if (CTemp.contains(".")) {
					CTemp = CTemp.substring(0, 2);
				}
				setTemperature = Integer.valueOf(CTemp);
			}
			Log.d("rawData", "288  eco = " + eco);
			if (tempUnit == IDataBodyDevStauts.TEMPERATURE_Celsius) {
				setTemperature -= 16;
				if (setTemperature < 1 || setTemperature > 14) {
					setTemperature = 1;
				}
				setTemperature_dot = 0x00;
			} else {
				setTemperature -= 16;
				if (setTemperature < 1 || setTemperature > 14) {
					setTemperature = 1;
				}
			}
			Log.d("rawData", "310  eco = " + eco);
			Secondbyte = (byte) ((((byte) mode << 5) & 0xE0) | ((byte) setTemperature & 0x0F)
					| (((byte) setTemperature_dot << 4) & 0x10));
			Thirdbyte = (byte) ((fanSpeed) | (timerEffe << 7 & 0x80));
			Fourthbyte = (byte) ((timer_on << 7 & 0x80) | (timer_on_hour << 2 & 0x7C) | (timer_on_minH & 0x03));
			Fifthbyte = (byte) ((timer_off << 7 & 0x80) | (timer_off_hour << 2 & 0x7C) | (timer_off_minH & 0x03));

			Sixthbyte = (byte) ((timer_off_minL & 0x0f) | (timer_on_minL << 4 & 0xf0));
			if ((timer_on << 7 & 0x80) == 0x00) {
				Fourthbyte = 0x7f;
				Sixthbyte &= 0x0f;
			}
			if ((timer_off << 7 & 0x80) == 0x00) {
				Fifthbyte = 0x7f;
				Sixthbyte &= 0xf0;
			}
			Log.d("rawData", "317  eco = " + eco);
			// nightLight = 1;
			// keyBtn = ((updownFan != 0) || (leftRightFan != 0)) ? (byte) 0x30
			// : 0x00;
			Seventhbyte = (byte) ((byte) 0x30 | ((leftRightFan != 0) ? 0x03 : 0x00) | ((updownFan != 0) ? 0x0C : 0x00));
			Eighthbyte = (byte) ((cosySleep & 0x03) | (alarmSleep << 2 & 0x04) | (save << 3 & 0x08)
					| (lowFerqFan << 4 & 0x10) | (tubro << 5 & 0x20) | (powerSaver << 6 & 0x40)
					| (feelOwn << 7 & 0x80));
			Ninethbyte = (byte) ((wiseEye & 0x01) | (exchangeAir << 1 & 0x02) | (dryClean << 2 & 0x04)
					| (ptcAssis << 3 & 0x08) | (ptcButton << 4 & 0x10) | (cleanUp << 5 & 0x20)
					| (changeCosySleep << 6 & 0x40) | (eco << 7 & 0x80));
			Tenthbyte = (byte) ((sleepFunc & 0x01) | (tubro << 1 & 0x02) | (tempUnit << 2 & 0x04)
					| (catchCold << 3 & 0x08) | (nightLight << 4 & 0x10) | (peakElec << 5 & 0x20)
					| (dusFull << 6 & 0x40) | (cleanFanTime << 7 & 0x80));

			Log.d("catchCold", "发送  " + catchCold);

			Log.d("ApplianceStatus", "Tenthbyte " + Tenthbyte + "   " + cleanFanTime);
			Eleventhbyte = 0x00;
			Twelfthbyte = 0x00;
			Thirteenthbyte = 0x00;
			Fourteenthbyte = 0x00;
			Fifteenthbyte = (byte) (naturalFan << 6 & 0x40);
			Sixteenthbyte = 0x00;
			Seventeenthbyte = 0x00;
			Eighteenthbyte = 0x00;
			Nineteenthbyte = 0x00;
			Twentieth = 0x00;
			Twenty_first = (byte) ((setExpand_dot & 0x01) | (setExpand << 1 & 0x3E) | (double_temp << 6 & 0x40)
					| (Eight_Hot << 7 & 0x80));

			updateProtocol(getProtocol());
			setBytes(bytes);
			byte[] b = addHead(bytes);
			return b;
		} else if (commandType == 0x41) {
			byte[] bytes = new byte[22];
			bytes[0] = Zerobyte = (byte) 0x41;
			bytes[1] = FirstByte = (byte) 0xC0;
			bytes[2] = Secondbyte = (byte) 0;
			bytes[3] = Thirdbyte = (byte) (powerUsage | readCmd);

			bytes[4] = Fourthbyte = (byte) setCmd;
			bytes[5] = Fifthbyte = (byte) clearCmd;
			bytes[6] = Sixthbyte = (byte) resetCmd;
			bytes[7] = Seventhbyte = (byte) addupCmd;
			bytes[8] = Eighthbyte = (byte) runmodeCmd;
			bytes[9] = Ninethbyte = (byte) setallusageCmd;

			bytes[10] = Tenthbyte = (byte) powerValue1;
			bytes[11] = Eleventhbyte = (byte) powerValue2;
			bytes[12] = Twelfthbyte = (byte) powerValue3;

			bytes[13] = Thirteenthbyte = (byte) time1;
			bytes[14] = Fourteenthbyte = (byte) timeLimit;
			bytes[15] = Fifteenthbyte = (byte) time3;

			number++;
			if (number == 3) {
				number = 4;
			}
			bytes[20] = number;
			bytes[21] = (byte) getCRC(bytes, bytes.length - 1);
			return addHead(bytes);
		}
		return null;
	}

	@Override
	public byte[] toBytes() {
		return toBytes(false);
	}

	private void setBytes(byte[] bytes) {
		bytes[0] = Zerobyte;
		bytes[1] = FirstByte;
		bytes[2] = Secondbyte;
		bytes[3] = Thirdbyte;
		bytes[4] = Fourthbyte;
		bytes[5] = Fifthbyte;
		bytes[6] = Sixthbyte;
		bytes[7] = Seventhbyte;
		bytes[8] = Eighthbyte;
		bytes[9] = Ninethbyte;
		bytes[10] = Tenthbyte;
		bytes[11] = Eleventhbyte;
		bytes[12] = Twelfthbyte;
		bytes[13] = Thirteenthbyte;
		bytes[14] = Fourteenthbyte;
		bytes[15] = Fifteenthbyte;
		bytes[16] = Sixteenthbyte;
		bytes[17] = Seventeenthbyte;
		bytes[18] = Eighteenthbyte;
		bytes[19] = Nineteenthbyte;
		bytes[20] = Twentieth;
		bytes[21] = Twenty_first;
		bytes[22] = 0x00;
		bytes[23] = (byte) number++;
		bytes[24] = (byte) getCRC(bytes.clone(), bytes.length - 1);

		Log.d("CRC8", "25 个bytes " + printHexString(bytes) + "CRC8 = " + printHexString(bytes[24]));

	}

	public void setDataBodyStatus(BaseDevice newStatu) {
		if (newStatu instanceof DeviceStatus) {
			DeviceStatus newStatus = (DeviceStatus) newStatu;
			powerStatus = newStatus.powerStatus;
			mode = newStatus.mode;
			setTemperature = newStatus.setTemperature;
			fanSpeed = newStatus.fanSpeed;
			/**** 定时关机 ****/
			timer_off = newStatus.timer_off;
			timer_off_hour = newStatus.timer_off_hour;
			timer_off_minH = (byte) (newStatus.timer_off_min / 15);
			timer_off_minL = (byte) (newStatus.timer_off_min % 15);
			/**** 定时开机 ****/
			timer_on = newStatus.timer_on;
			timer_on_hour = newStatus.timer_on_hour;
			timer_on_minH = (byte) (newStatus.timer_on_min / 15);
			timer_on_minL = (byte) (newStatus.timer_on_min % 15);

			/**** 特殊功能 ****/
			feelOwn = newStatus.feelOwn;
			powerSaver = newStatus.powerSaver;
			tubro = newStatus.tubro;
			lowFerqFan = newStatus.lowFerqFan;
			save = newStatus.save;
			alarmSleep = newStatus.alarmSleep;
			cosySleep = newStatus.cosySleep;

			eco = newStatus.eco;
			cleanUp = newStatus.cleanUp;// 净化
			ptcButton = newStatus.ptcButton;
			ptcAssis = newStatus.ptcAssis;
			dryClean = newStatus.dryClean;
			exchangeAir = newStatus.exchangeAir;
			wiseEye = newStatus.wiseEye;

			sleepFunc = newStatus.sleepFunc;
			catchCold = newStatus.catchCold;

			Log.d("catchCold", "设置  " + catchCold + "     " + newStatus.catchCold);

			nightLight = newStatus.nightLight;
			peakElec = newStatus.peakElec;

			naturalFan = newStatus.naturalFan;
			pmvFunc_H = newStatus.pmvFunc_H;
			pmvFunc_L = newStatus.pmvFunc_L;

			leftRightFan = newStatus.leftRightFan;
			updownFan = newStatus.updownFan;
			superFan = newStatus.superFan;
			cleanFanTime = newStatus.cleanFanTime;
			tempUnit = newStatus.tempUnit;

			Eight_Hot = newStatus.Eight_Hot;

			/**** 41命令 ****/
			setCmd = newStatus.setCmd;
			clearCmd = newStatus.clearCmd;
			resetCmd = newStatus.resetCmd;
			addupCmd = newStatus.addupCmd;
			runmodeCmd = newStatus.runmodeCmd;
			setallusageCmd = newStatus.setallusageCmd;
			powerValue1 = newStatus.powerValue1;
			powerValue2 = newStatus.powerValue2;
			powerValue3 = newStatus.powerValue3;
			time1 = newStatus.time1;
			timeLimit = newStatus.timeLimit;
			time3 = newStatus.time3;
		}
	}

	@Override
	public Object toObject(byte[] bytes) {
		// TODO Auto-generated method stub
		return null;
	}

}
